package com.qubuyer.business.home.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.blankj.utilcode.util.ConvertUtils
import com.qubuyer.R
import com.qubuyer.business.home.adapter.HomeCategoryAdapter
import com.qubuyer.business.home.adapter.HomeViewPagerAdapter
import com.qubuyer.databinding.HomeItemCateBinding
import com.qubuyer.repository.entitys.HomeCategoryEntity
import com.qubuyer.utils.PageUtil
import java.util.*
import kotlin.collections.ArrayList

class CateItemView(lifecycleOwner: LifecycleOwner, context: Context, parent: ViewGroup, layoutId: Int) : HomeItemView<List<HomeCategoryEntity>, HomeItemCateBinding>(lifecycleOwner, context,parent, layoutId) {

    override fun isTheSameData(origin: List<HomeCategoryEntity>, now: List<HomeCategoryEntity>): Boolean {
        if (origin.size != now.size) {
            return false
        }
        val size = origin.size
        for (i in 0 until size) {
            if (origin[i] != now[i]) {
                return false
            }
        }
        return true
    }

    override fun bindView(data: List<HomeCategoryEntity>) {
        val pageList: MutableList<View> = ArrayList()
        val pageUtil = PageUtil(data, 10)
        dataBinding.llIndicator.removeAllViews()
        if (pageUtil.getTotalPages() > 1) {
            dataBinding.llIndicator.visibility = View.VISIBLE
            var view: View
            for (i in 0 until pageUtil.getTotalPages()) {
                view = View(mContext)
                val layoutParams1 = LinearLayout.LayoutParams(ConvertUtils.dp2px(13f), ConvertUtils.dp2px(3f))
                layoutParams1.setMargins(ConvertUtils.dp2px(5f), 0, ConvertUtils.dp2px(5f), 0)
                view.layoutParams = layoutParams1
                view.setBackgroundResource(R.drawable.home_cate_indi_slt)
                if (i == 0) {
                    view.isSelected = true
                }
                dataBinding.llIndicator.addView(view)
            }
        } else {
            dataBinding.llIndicator.visibility = View.GONE
        }
        for (i in 0 until pageUtil.getTotalPages()) {
            val page = LayoutInflater.from(mContext).inflate(R.layout.layout_fragment_home_category_page, null).apply {
                findViewById<RecyclerView>(R.id.rv_list).apply {
                    layoutManager = GridLayoutManager(mContext, 5)
                    adapter = HomeCategoryAdapter(ArrayList(pageUtil.getObjects(i + 1)))
                }
            }
            pageList.add(page)
        }
        val mViewPagerAdapter = HomeViewPagerAdapter(pageList)
        dataBinding.viewPager.adapter = mViewPagerAdapter
        dataBinding.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                val count = dataBinding.llIndicator.childCount
                for (i in 0 until count) {
                    dataBinding.llIndicator.getChildAt(i).isSelected = i == position
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        dataBinding.viewPager.currentItem = 0
    }
}